package com.tanhua.server.controller;

import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UsersService;
import com.tanhua.server.vo.CountsVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/3/1 17:19
 * @version: 0.0.1
 * @modified By:
 */
@RestController
@RequestMapping("users")
public class UsersController {
    @Autowired
    private UsersService usersService;
    /**
     * @description 查询用户信息
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo>queryUserInfo(@RequestParam(value = "userID",required = false)Long userId,
                                                   @RequestParam(value = "huanxinID",required = false)Long huanxinID){
        try {
            UserInfoVo userInfoVo= this.usersService.queryUserInfo(userId,huanxinID);
            if(null!=userInfoVo){
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description  用户资料保存
     * @author
     * @date
     * @param
     *@return
     */
    @PutMapping()
    public ResponseEntity<Void>updateUserInfo(@RequestBody UserInfoVo userInfoVo){
        try {
            Boolean bool= this.usersService.updateUserInfo(userInfoVo);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 互相喜欢、喜欢、粉丝
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("counts")
    public ResponseEntity<CountsVo>queryCounts(){
        try {
            CountsVo countsVo=this.usersService.queryCounts();
            if(null!=countsVo){
                return ResponseEntity.ok(countsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 互相喜欢、喜欢、粉丝、谁看过我
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity<PageResult>queryLikeList(@PathVariable("type")Integer type,
                                               @RequestParam(value = "page",defaultValue = "1")Integer page,
                                               @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
                                               @RequestParam(value = "nickname",required = false)String nickName){
        try {
            page=Math.max(1,page);
            PageResult pageResult=this.usersService.queryLikeList(type,page,pageSize,nickName);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 取消喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity<Void>disLikeUser(@PathVariable("uid")Long userId){
        try {
            Boolean bool=this.usersService.disLikeUser(userId);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 粉丝喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity<Void>saveLikeFan(@PathVariable("uid")Long userId){
        try {
            Boolean bool= this.usersService.saveLikeFan(userId);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 用户通用设置读取
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("settings")
    public ResponseEntity<SettingsVo>querySettings(){
        try {
            SettingsVo settingsVo= this.usersService.querySettings();
            if(null!=settingsVo){
                return ResponseEntity.ok(settingsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 设置陌生人问题
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping("questions")
    public ResponseEntity<Void>saveStrangerQuestion(@RequestBody Map<String,Object>param){
        try {
            String content = param.get("content").toString();
            Boolean bool=this.usersService.saveStrangerQuestion(content);
            if(bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 黑名单列表
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("blacklist")
    public ResponseEntity<PageResult>queryBlackList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                    @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult=this.usersService.queryBlackList(page,pageSize);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 移除黑名单
     * @author
     * @date
     * @param
     *@return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity<Void>deleteBlackList(@PathVariable("uid")Long userId){
        try {
            Boolean bool= this.usersService.deleteBlackList(userId);
            if(bool){
                return  ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
